using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class GameTool 
{
    private static GameTool instance;

    public static GameTool Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new GameTool();
            }
            return instance;
        }
    }

    private GameObject canvas;
    private GameTool()
    {
        canvas = GameObject.Find("MCanvas");
        EventCenter.Instance.RegisterObserver(EventType.OnSceneChangeComplete, () =>
        {
            canvas = GameObject.Find("MCanvas");
        });
    }
    public GameObject GetGameObjectFromCanvas(string name)
    {
        foreach (Transform obj in canvas.GetComponentsInChildren<Transform>(true))
        {
            if (obj.name == name)
            {
                return obj.gameObject;
            }
        }

        return null;
    }
    public GameObject GetGameObjectFromChildren(GameObject parent, string name, bool inActive)
    {
        foreach (Transform obj in parent.GetComponentsInChildren<Transform>(inActive))
        {
            if (obj.name == name)
            {
                return obj.gameObject;
            }
        }
        return null;
    }
    public GameObject GetGameObjectFromChildren(GameObject parent, string name)
    {
        return GetGameObjectFromChildren(parent, name, true);
    }
    public T GetComponentFromChild<T>(GameObject parent, string name)
    {
        return GetGameObjectFromChildren(parent, name).GetComponent<T>();
    }
    public GameObject GetMainCanvas()
    {
        return canvas;
    }
    public void ReFindCanvas()
    {
        canvas = GameObject.Find("MCanvas");
    }
}
